<style>
    table{
        border: 4px double red;
        border-spacing: 0;
    }
    th,td{
        border-bottom: 1px dotted red;
        border-right: 1px dotted red;
        padding: 10px;
    }
</style>

<?php
$db = new PDO("mysql:host=localhost",'root','root');
$db -> exec('use db3');
//获取查询结果  foreach遍历     fetch遍历
//语句对象的fetch方法可从结果集中获取下一条记录，返回数组，失败时返回false
echo "========================================使用fetch获取一行记录的数组===================================\n";
$ps = $db -> query('select name from students limit 1');
var_dump($ps ->fetch());
var_dump($ps ->fetch());
echo "========================================使用while循环，fetch获取数据集的所有记录===================================\n";
$ps = $db -> query('select * from students');
while ($row = $ps -> fetch()){
    echo "1.{$row['xh']}\t{$row['name']}\t{$row['age']}\n";
}
//fetchColumn方法获取某列的值    从结果集的下一行返回单独的一列，列号从0开始，默认值是0列
echo "========================================使用fetchColumn获取下一条记录某列的值===================================\n";
$ps = $db -> query('select count(*) from students');
echo "记录个数为：{$ps -> fetchColumn()}\n";
//fetchAll方法获取所有结果集中的所有记录      返回一个包含结果集中所有行的数组
echo "========================================使用fetchAll获取所有结果集中的所有记录，以数组的形式返回===================================\n";
$ps = $db -> query('select * from students');
$rs = $ps -> fetchAll();
//print_r($rs);
echo "2.{$rs[0]['xh']}\t{$rs[1]['name']}\t{$rs[2]['age']}\n";
//提取数据模式
//fetch和fetchAll都可以带一个提取数据模式的参数
echo "========================================使用数据的提取模式 FETCH_ASSOC===================================\n";
$ps = $db -> query('select * from students limit 1');
$rs = $ps -> fetchAll(PDO::FETCH_ASSOC);//得到的是一个数组
print_r($rs);

//创建PDO对象时直接设置
//$db = new PDO("mysql:host=localhost",'root','root',[PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC]);
//echo "------------------------------------------\n";

//创建PDO对象后设置
//$db = new PDO("mysql:host=localhost",'root','root');
$db -> setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE,PDO::FETCH_ASSOC);
echo "------------------------------------------\n";

//利用PDO::query查询时
//$db = new PDO("mysql:host=localhost",'root','root');
$ps = $db -> query('select * from students limit 1',PDO::FETCH_ASSOC);
print_r($ps -> fetch());
echo "------------------------------------------\n";

//利用语句对象进行查询时
//$db = new PDO("mysql:host=localhost",'root','root');
$ps = $db -> prepare('select * from students where xh=?');
$ps -> setFetchMode(PDO::FETCH_ASSOC);
$ps -> execute(['213110601']);
print_r($ps -> fetch());
echo "------------------------------------------\n";

//利用语句对象fetch对象时
//$db = new PDO("mysql:host=localhost",'root','root');
$ps = $db -> prepare ('select * from students where xh=?');
$ps -> execute(['213110601']);
print_r($ps -> fetch(PDO::FETCH_ASSOC));


//利用表格显示表数据
//columnCount()可以获取表的字段数，若结果集不存在，则返回0
//getColumnMeta()可以获取每一个字段的元数据，比如字段名
echo "========================================利用table显示数据=================================\n";
$ps = $db -> query('select * from students limit 1');
var_dump("字段个数：".$ps -> columnCount());
$ps = $db -> prepare('select * from students');
$ps -> execute();
var_dump("字段个数：".$ps -> columnCount());
print_r($ps -> getColumnMeta(0));
table($ps);
function table($ps){
    $count = $ps -> columnCount();
    if ($count == 0) return;//如果$ps中没有结果集，则不用显示，直接返回
    echo "<table><tr>";
    for ($i = 0;$i<$count;++$i){
        echo "<th>{$ps -> getColumnMeta($i)['name']}</th>";
    }
    echo "</tr>";
    foreach ($ps as $r){//循环显示每一条记录
        echo "<tr>";
        for ($j = 0;$j<$count;++$j) {
            echo "<td>",$r[$j] ?? $r[$ps -> getColumnMeta($j)['name']],"</td>";
        }
        echo "</tr>";
    }
    echo "</table>";
}

$db -> exec('use db');
$ps = $db -> query('select * from chengyu Limit 5');
table($ps);